function imExpSaverFunc(stimFileNames, imExpName, imExp, imExpType)
% imExpSaver saves an imExp from the imExpMaker gui. It implements
% uiputfile to save to a user specified location
% INPUTS:           stimFileNames: list of stimFileNames for constructing
%                                  imExp base name
%                   Exp:           Experiment structure created through the
%                                  imExpMaker gui
%                   imExpType:     the type of imExp to be saved (currently
%                                  there are three options; raw, roi,
%                                  and analyzed types. Raw type is handled
%                                  created by imExpMaker, roi type is
%                                  created by the imExpAnalyzer and
%                                  analyzed type is created by analyzer
%                                  scripts contained in the dir imSignal
%                                  Analysis
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%copyright (c) 2012  Matthew Caudill
%
%this program is free software: you can redistribute it and/or modify
%it under the terms of the gnu general public license as published by
%the free software foundation, either version 3 of the license, or
%at your option) any later version.
%
%this program is distributed in the hope that it will be useful,
%but without any warranty; without even the implied warranty of
%merchantability or fitness for a particular purpose.  see the
%gnu general public license for more details.
%
%you should have received a copy of the gnu general public license
%along with this program.  if not, see <http://www.gnu.org/licenses/>.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%% LOAD DIRECTORY INFORMATION %%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ImExpDirInformation
% depending on the type of imExp to be saved, we have different saving
% locations as specified in ImExpDirInformation. We use a switch case to
% determine the target location for our save.
switch imExpType
    case 'raw'
        expTargetLoc = dirInfo.imExpRawFileLoc;
    case 'roi'
        expTargetLoc = dirInfo.imExpRoiFileLoc;
    case 'analyzed'
        expTargetLoc = dirInfo.imExpAnalyzedFileLoc;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%% CONSTRUCT DEFAULT NAME FOR EXP %%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Depending on whether the imExpType is raw, roi, or analyzed, we need to
% construct a default save file name. If the imExp is type raw (meaning
% created by the imExpMaker), we must use the stimulus File Names to create
% an imExp Name. If the imExp being created is type roi or analyzed then
% we create the imExp name from the pre-existing imExp name generated by
% the imExpMaker gui

switch imExpType
    
    case 'raw' % The imExpMaker is calling the saver and we need to
               % construct an imExpName from the stimulus fileNames
        
        % split the stimFileName on the underscores returning an allStrings
        % cell array
        allStrings = regexp(stimFileNames{1},'_', 'split');
        % obtain the date
        date = allStrings{2};
        % obtain the location and expType identifier
        locExpType = allStrings{3};
        % lastly construct the default name
        defaultName =...
            [expTargetLoc, date,'_',locExpType,'_','imExp','.mat'];
    
    case {'roi', 'analyzed'} % either the imExpAnalyzer or an analysis 
                             % script is calling. So we use the
                             % pre-existing imExp name to create a new
                             % imExpName
                             
        % split the imExpName on the underscores
        allStrings = regexp(imExpName,'_', 'split');
        % obtain the date
        date = allStrings{1};
        % obtain the location and expType identifier
        locExpType = allStrings{2};
        
        % Now based on whether roi or analyzed type we construct a
        % different save file name
        if strcmp(imExpType, 'roi')
            defaultName = [expTargetLoc, date,'_', locExpType, '_',...
                'imExp_ROI', '.mat'];
            
        elseif strcmp(imExpType, 'analyzed')
            defaultName = [expTargetLoc, date,'_', locExpType, '_',...
               'imExp_Analyzed', '.mat'];
        end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%% CONSTRUCT PATH AND FILE NAME %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Use uiputfile to allow user to save to a directory of their choice
% suggesting the default name as the save name (note they can overide with
% their own name if they wish)
[fileName,pathName] = uiputfile(expTargetLoc,'Save As',defaultName);

file = fullfile(pathName,fileName);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% SAVE %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Depending on the size of the imExp, we will either save the file with the
% -v7.3 switch or with -v7 (standard). This is because the -v7.3 will allow
% us to save files larger than 2GB but is unfortunately much slower.
imExpInfo = whos('imExp');

byteSize = imExpInfo.bytes;

if ischar(fileName) && ischar(pathName)
    saveMsg = msgbox('SAVING: Please Wait...');
    
    if byteSize < 2.147e+9
    save(file, '-struct', 'imExp', '-v7');
    else % we do it the slower way
    save(file, '-struct', 'imExp', '-v7.3');
    end
    
    close(saveMsg)
else
    warndlg('WARNING: EXPERIMENT NOT SAVED')
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

end

